//
//  LoginModel.m
//  VideoExam
//
//  Created by apple on 2020/4/7.
//

#import "LoginModel.h"
#import "RSA.h"
#import "ZSJPushManager.h"
@implementation LoginModel

- (instancetype)init
{
    self = [super init];
    if (self) {
         _time=59;
        _Agree=NO;
        _canClicked = NO;
    }
    return self;
}
/// 发送验证码
/// @param phone 手机号
/// @param success success
/// @param errorBlock errorBlock
+(void)getCodePhone:(NSString *)phone  Success:(ObjSuccess)success error:(ErrorBlock)errorBlock{
//    NSMutableDictionary *dic = [NSMutableDictionary new];
//    [dic setObject:phone forKey:@"phone"];
   NSString * url = [NSString stringWithFormat:@"%@%@",get_Code_Url,phone];
    [NetWorkTools requestWithURLStr:url method:GET params:nil success:^(id responseObject) {
        NSLog(@"获取验证码 %@",responseObject);
        if ([AppTools isStateSuccess:responseObject]) {
            if (success) {
                success(nil);
            }
        } else {
            if (errorBlock) {
                errorBlock();
            }
        }
    } failure:^(NSError *error) {
        NSLog(@"获取验证码error %@",error);
        if (errorBlock) {
            errorBlock();
        }
        NSString *errorStr = [self errorDescribe:error];
        [QMUITips showError:errorStr];
    }];
}

/// 账号密码登录 ，验证码登录
/// @param account account
/// @param code 验证码
/// @param success success
/// @param errorBlock errorBlock
+(void)loginAccount:(NSString *)account  Code:(NSString *)code  Success:(ObjSuccess)success error:(ErrorBlock)errorBlock {
    NSString * registrationId =  [ZSJPushManager shareManager].registrationId;
    NSString * logUrl = [NSString stringWithFormat:@"%@/%@/%@/%@",login_Url,account,code,registrationId];
    [NetWorkTools requestWithURLStr:logUrl method:GET params:nil success:^(id responseObject) {
        NSLog(@"登录 %@",responseObject);
        if ([AppTools isStateSuccess:responseObject]) {
            NSString * token = responseObject[@"data"][@"value"];
           UserModel *model = [[UserModel alloc] init];
           model.phone=account;
           model.token=token;
           [UserModel save_userModel:model];
           [AppTools sharedInstance].token=token;
           [NetWorkTools sharedHttpSession];
            if (success) {
                success(nil);
            }
        } else {
            if (errorBlock) {
                 errorBlock();
             }
        }
        
    } failure:^(NSError *error) {
        NSLog(@"登录失败 %@",error);
          if (errorBlock) {
              errorBlock();
          }
          NSString *errorStr = [self errorDescribe:error];
          [QMUITips showError:errorStr];
    }];

}

/// RSA 加密
/// @param content 要加密的字符串
+ (NSString *)encryption_content:(NSString *)content {
    //公钥
       NSString *publicKey = @"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqFXZhV1tewJqwT8a88OxIhV8LB54ZE6UZFHa26/s9wDPcmu0QVA2FxqMv/aWp5a/WBGKyuGjzYr5jywHriAyUtkV3nye0ocXeFP8hHPzRj7HQzmpKKEqAQqMKg0iQp/hmIQDGXb6dR3gI9EpT61NbaaXb8AIvUQarEIRLIGEQZAr2pehA9xNP14wz55w84zz4TPMfPJhroVYDVY0Pies3oNd4ZlxCJa4Bf+w5XXriUiGbAidkD9IlaK9J4XhVFgidoSxNZcT05evQ76mcLWrCQNXTWksupOH3nGTwob8k9YbJoivUcZTWSBvBB7keVTBbLKzNF9KCz9YSHKdsiAsuQIDAQAB";
    //公钥加密
      NSString *encryptStr = [RSA encryptString:content publicKey:publicKey];
    
    return encryptStr;
}

@end
